package codeTop;

public class q48 {
    public void rotate(int[][] matrix) {
        int n = matrix.length;
        boolean[][] visited = new boolean[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (!visited[i][j]){
                    func(matrix,visited,i,j);
                }
            }
        }
    }

    private void func(int[][] matrix, boolean[][] visited, int i, int j) {
        int n = matrix.length;
        int temp = matrix[i][j];
        matrix[i][j] = matrix[n-1-j][i];
        matrix[n-1-j][i] = matrix[n-1-i][n-1-j];
        matrix[n-1-i][n-1-j] = matrix[j][n-1-i];
        matrix[j][n-1-i] = temp;
        visited[i][j] = true;
        visited[n-1-j][i] = true;
        visited[n-1-i][n-1-j] = true;
        visited[j][n-1-i] = true;
    }
}
